package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by maomao
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;
    @RequestMapping(value = "test.do",method = RequestMethod.GET)
    @ResponseBody
    public String  test(){
        return "test";
    }
    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    //用户登录
    // Service===>Mybatis===>Dao
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User>  login(String username, String password, HttpSession session){

        ServerResponse<User> response = iUserService.login(username,password);

        if(response.isSuccess()){

            session.setAttribute(Const.CURRENT_USER,response.getData());

        }

        return response;
    }

    //用户登出
    // Service===>Mybatis===>Dao
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User>  logout(HttpSession session){

            session.removeAttribute(Const.CURRENT_USER);

            return ServerResponse.createBySuccess();

    }

    //用户注册
    // Service===>Mybatis===>Dao
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String>  register(User user){

        return iUserService.register(user);

    }

    //用户校验
    // Service===>Mybatis===>Dao
    @RequestMapping(value = "checkvalid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String>  checkValid(String str,String type){

        return iUserService.checkValid(str,type);

    }

    // 获取用户信息

    /**
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "getinfo.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户没有登录，无法获取当前用户的信息");
    }
    /**
     * 忘记密码
     * @param username
     * @return
     */

    @RequestMapping(value = "forgetgetquestion.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){

        return iUserService.selectQuestion(username);
    }

    /**
     * 校验
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetCheckAnser(String username,String question,String answer){

        return iUserService.checkAnswer(username,question,answer);

    }

    /**
     * 忘记且重置密码
     * @param username
     * @param passwordnew
     * @param forgetToken
     * @return
     */

    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordnew,String forgetToken){

        return iUserService.forgetResetPassword(username,passwordnew,forgetToken);

    }


    // 登录状态的重置密码

    /**
     *
     * @param session
     * @param passwordnew
     * @param passwordold
     * @return
     */

    @RequestMapping(value = "reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String passwordold,String passwordnew){

        User user = (User)session.getAttribute(Const.CURRENT_USER);

        if(user == null){

            return ServerResponse.createByErrorMessage("用户没有登录");
        }

        return iUserService.resetPassword(passwordold,passwordnew,user);

    }

    //更新用户信息



    @RequestMapping(value = "update_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> update_information(HttpSession session, User user){

        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);

        if(currentUser == null){

            return ServerResponse.createByErrorMessage("用户没有登录");
        }   // 只有在登录状态下才能修改个人信息


        user.setId(currentUser.getId());

        user.setUsername(currentUser.getUsername());

        ServerResponse<User> response = iUserService.updateInformation(user);

        if(response.isSuccess()){

            session.setAttribute(Const.CURRENT_USER,response.getData());

        }

        return response;

    }

    @RequestMapping(value = "get_information.do",method = RequestMethod.POST)
    @ResponseBody

    public ServerResponse<User> get_information(HttpSession session){

        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);

        if(currentUser == null){

            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"没有登录,需要强制登录 status= 10");
        }   // 只有在登录状态下才能修改个人信息

        return iUserService.getInformation(currentUser.getId());

    }

}
